package com.ming.listens;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class RabbitmqListen {
    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String message){
        System.out.println(String.format("消费者收到了simple.queue: %s", message));
    }

    @RabbitListener(queues = "work.queue")
    public void listenWorkQueue1(String message){
        System.out.println(String.format("消费者1，收到了work.queue: %s", message));
    }

    @RabbitListener(queues = "work.queue")
    public void listenSimpleQueue2(String message) throws InterruptedException {
        System.err.println(String.format("消费者2，收到了work.queue: %s", message));
        Thread.sleep(2000);
    }

    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String message) {
        System.out.println(String.format("消费者1，收到了fanout.queue1: %s", message));
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String message) {
        System.err.println(String.format("消费者2，收到了fanout.queue2: %s", message));
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "fanout.queue5", durable = "true"),
            exchange = @Exchange(name = "mt.fanout3", type = ExchangeTypes.FANOUT)
    ))
    public void listenFanoutQueue3(String message) {
        System.out.println(String.format("消费者3，收到了fanout.queue3: %s", message));
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "fanout.queue6", durable = "true"),
            exchange = @Exchange(name = "mt.fanout3", type = ExchangeTypes.FANOUT)
    ))
    public void listenFanoutQueue4(String message) {
        System.err.println(String.format("消费者4，收到了fanout.queue4: %s", message));
    }

    @RabbitListener(queues = "direct.queue1")
    public void listenDirecttQueue1(String message) {
        System.out.println(String.format("消费者1，收到了direct.queue1: %s", message));
    }

    @RabbitListener(queues = "direct.queue2")
    public void listenDirecttQueue2(String message) {
        System.err.println(String.format("消费者2，收到了direct.queue2: %s", message));
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue4", durable = "true"),
            exchange = @Exchange(name = "mt.direct3", type = ExchangeTypes.DIRECT),
            key = {"red", "blue"}
    ))
    public void listenDirecttQueue3(String message) {
        System.err.println(String.format("消费者3，收到了direct.queue3: %s", message));
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue5", durable = "true"),
            exchange = @Exchange(name = "mt.direct3", type = ExchangeTypes.DIRECT),
            key = {"red", "yellow"}
    ))
    public void listenDirecttQueue4(String message) {
        System.err.println(String.format("消费者4，收到了direct.queue4: %s", message));
    }

    @RabbitListener(queues = "topic.queue1")
    public void listenTopicQueue1(String message) {
        System.out.println(String.format("消费者1，收到了topic.queue1: %s", message));
    }

    @RabbitListener(queues = "topic.queue2")
    public void listenTopicQueue2(String message) {
        System.err.println(String.format("消费者2，收到了topic.queue2: %s", message));
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue4", durable = "true"),
            exchange = @Exchange(name = "mt.topic3", type = ExchangeTypes.TOPIC),
            key = {"china.#", "#.news"}
    ))
    public void listenTopicQueue3(String message) {
        System.out.println(String.format("消费者3，收到了topic.queue3: %s", message));
    }

    @RabbitListener(queues = "object.queue")
    public void listenObjectQueue(Map<String, Object> message){
        System.out.println(String.format("消费者收到了object.queue: %s", message));
    }
}
